<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="时间范围" prop="datePicker">
              <el-date-picker v-model="datePicker" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" @change="changeTimer" />
            </el-form-item>
            <el-form-item label="短剧名称" prop="dramaTitle">
              <el-input v-model="queryParams.dramaTitle" placeholder="请输入短剧名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery" v-hasPermi="['oce:advertiserEverydayCost:listDrama']">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery" v-hasPermi="['oce:advertiserEverydayCost:listDrama']">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>
    <el-card shadow="never">
      <listView :list="sum" />
      <el-table v-loading="loading" :data="materialEverydayCensusList" @sort-change="sortChangeHandel" @selection-change="handleSelectionChange">
        <el-table-column fixed label="短剧名称" prop="dramaTitle" align="center"></el-table-column>

        <el-table-column label="账户消耗" prop="cost" align="center" sortable="custom">
          <template #default="{ row }">
            <span>{{ formatPrice(row.cost) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="当日充值" prop="thatdayTotalAmount" align="center" sortable="custom">
          <template #default="{ row }">
            <span>{{ formatPrice(row.thatdayTotalAmount) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="分成金额" prop="thatdayDividendAmount" align="center" sortable="custom">
          <template #default="{ row }">
            <span>{{ formatPrice(row.thatdayDividendAmount) }}</span>
          </template>
        </el-table-column>

        <el-table-column label="当日广告收益" prop="iaaTotalAmount" align="center" sortable="custom">
          <template #default="{ row }">
            <span>{{ formatPrice(row.iaaTotalAmount) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="当日广告分成" prop="iaaDividendAmount" align="center" sortable="custom">
          <template #default="{ row }">
            <span>{{ formatPrice(row.iaaDividendAmount) }}</span>
          </template>
        </el-table-column>

        <el-table-column label="利润" prop="thatdayProfitAmount" align="center" sortable="custom">
          <template #default="{ row }">
            <span>{{ formatPrice(row.thatdayProfitAmount) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="回本率" prop="extRecoveryRate" align="center" sortable="custom">
          <template #default="{ row }">
            <span>{{ formatPrice(row.extRecoveryRate) }}%</span>
          </template>
        </el-table-column>
        <el-table-column label="转化数" prop="convert" align="center" sortable="custom">
          <template #default="{ row }">
            <span>{{ formatPrice(row.convert) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="转化成本" prop="convertCost" align="center" sortable="custom">
          <template #default="{ row }">
            <span>{{ formatPrice(row.convertCost) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="付费人数" prop="thatdayPaidUseNum" align="center" sortable="custom">
          <template #default="{ row }">
            <span>{{ row.thatdayPaidUseNum }}</span>
          </template>
        </el-table-column>
        <el-table-column label="付费次数" prop="gamePayCount" align="center" sortable="custom">
          <template #default="{ row }">
            <span>{{ formatPrice(row.gamePayCount) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="MaterialEverydayCensus" lang="ts">
import { listDramar } from '@/api/oce/playletStatistics';
import { AdvertiserEverydayCostVO } from '@/api/oce/playletStatistics/types';
import { formatYmd, formatPrice } from '@/utils';
import dayjs from 'dayjs';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const materialEverydayCensusList = ref<AdvertiserEverydayCostVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const data = reactive<PageData<AdvertiserEverydayCostVO, AdvertiserEverydayCostVO>>({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    thatdayTimestamp: undefined,
    thatdayDate: undefined,
    cost: undefined,
    thatdayTotalAmount: undefined,
    thatdayPaidUseNum: undefined,
    gamePayCount: undefined,
    iaaTotalAmount: undefined,
    iaaDividendAmount: undefined,
    thatdayDividendAmount: undefined,
    thatdayProfitAmount: undefined,
    iaaRegisterTotalAmount: undefined,
    iaaRegisterDividendAmount: undefined,
    extRecoveryRate: undefined,
    convert: undefined,
    convertCost: undefined,
    returnRate: undefined,
    orderByColumn: undefined,
    isAsc: undefined,
    dramaTitle: undefined,
    createName: undefined,
    params: {},
    advertiserCostId: undefined,
    appType: undefined,
    referralLinkId: undefined,
    referralLinkType: undefined,
    thirdAdvertiserAppCode: undefined,
    thirdAdvertiserId: undefined
  }
});

const { queryParams, form, rules } = toRefs(data);
const datePicker = ref<any>([dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]);
const sum = ref([]);

/** 查询巨量素材每日数据统计列表 */
const getList = async () => {
  loading.value = true;
  const res = await listDramar(queryParams.value);
  materialEverydayCensusList.value = res.rows;
  sum.value = res.sum || [];
  total.value = res.total;
  loading.value = false;
};

const sortChangeHandel = ({ order, prop }) => {
  queryParams.value.orderByColumn = prop;
  queryParams.value.isAsc = order === 'ascending' ? 'asc' : 'desc';
  getList();
};

const changeTimer = (e) => {
  queryParams.value.startDate = e ? formatYmd(e[0]) : '';
  queryParams.value.endDate = e ? formatYmd(e[1]) : '';
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  datePicker.value = [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')];
  queryParams.value.startDate = datePicker.value[0];
  queryParams.value.endDate = datePicker.value[1];
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: AdvertiserEverydayCostVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    '/oce/advertiserEverydayCost/exportPitcher',
    {
      ...queryParams.value
    },
    `materialEverydayCensus_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  queryParams.value.startDate = datePicker.value[0];
  queryParams.value.endDate = datePicker.value[1];
  getList();
});
</script>
